<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 10:16
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\AdminModel;
use App\Http\Model\AssignmentModel;
use App\Http\Model\ProfessionModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ProfessionController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:1',
        ];
        $message = [
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length','page');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $professionModel = new ProfessionModel();
            $assignmentModel = new AssignmentModel();
            $lists= $professionModel->lists($param);
            $countLists = $professionModel->countLists();
            foreach ($this->data as  $key => $list) {
                $assignment = $assignmentModel->countLists(['professionId'=>$list->id]);
                if ($assignment>0){
                    $this->data[$key]->hasAssignment = 1;
                }else{
                    $this->data[$key]->hasAssignment = 0;
                }
            }
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(),240101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 根据账号的负责工种生成工种下拉列表
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function selectLists(Request $request)
    {
        $professionModel = new ProfessionModel();
        $adminId = $request->session()->get('adminId');
        $info = (new AdminModel())->info(['id' => $adminId]);
        $this->data = $professionModel->selectLists(json_decode($info['professionId'], true));
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工种参数失败',
            'id.integer' => '工种参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $professionModel = new ProfessionModel();
            $this->data = $professionModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(),240201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'order' => 'integer',
            'isManager' => 'required|integer|in:0,1',
            'isMajor' => 'required|integer|in:0,1',
        ];
        $message = [
            'name.required' => '请输入工种名称',
            'order.integer' => '工种排序数据类型不正确',
            'isManager.required' => '请选择是否为管理人员',
            'isManager.integer' => '是否为管理人员参数类型错误',
            'isManager.in' => '是否为管理人员参数值错误',
            'isMajor.required' => '请选择是否为专业班组',
            'isMajor.integer' => '是否为专业班组参数类型错误',
            'isMajor.in' => '是否为专业班组参数值错误',
        ];
        $param = $request->only(['name', 'order', 'isManager', 'isMajor']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $professionModel = new ProfessionModel();
            $info = $professionModel->checkRepeat(['name' => $param['name']]);
            if (!empty($info)) {
                throw new ApiException('已经存在同名工种',240302);
            }
            $newId = $professionModel->insert($param);
            if (!$newId) {
                throw new ApiException('保存失败，请稍后重试',240301);
            }
        } else {
            throw new ApiException($validator->errors()->first(),240301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'order' => 'integer',
            'isManager' => 'required|integer|in:0,1',
            'isMajor' => 'required|integer|in:0,1',

        ];
        $message = [
            'id.required' => '获取工种参数失败',
            'id.integer' => '工种参数类型错误',
            'name.required' => '请输入工种名称',
            'order.integer' => '工种排序数据类型不正确',
            'isManager.required' => '管理人员获取参数失败',
            'isManager.integer' => '管理人员参数类型错误',
            'isManager.in' => '管理人员参数值错误',
            'isMajor.required' => '请选择是否为专业班组',
            'isMajor.integer' => '是否为专业班组参数类型错误',
            'isMajor.in' => '是否为专业班组参数值错误',
        ];
        $param = $request->only(['id', 'name', 'order', 'isManager','isMajor']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $professionModel = new ProfessionModel();
            $info = $professionModel->checkRepeat(['name' => $param['name']], $param['id']);
            if (!empty($info)) {
                throw new ApiException('已经存在同名工种',240402);
            }
            if (!isset($param['order']) || empty($param['order'])){
                $order = 1;
            }else{
                $order = $param['order'];
            }
            $updateData = [
                'name' => $param['name'],
                'order' => $order,
                'isManager' => $param['isManager'],
                'isMajor' => $param['isMajor'],
            ];
            $professionModel->update(['id'=>$param['id']],$updateData);
        } else {
            throw new ApiException($validator->errors()->first(),240401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取工种参数失败',
            'id.integer' => '工种参数类型错误',
            'status.required' => '未获取到状态',
            'status.integer' => '工种状态参数类型错误',
            'status.in' => '工种状态参数值不正确',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $professionModel = new ProfessionModel();
            $professionModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(),240501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 专业工种下如果没有施工项，可以复制其他工种的施工项
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function copyAssignment(Request $request){
        $rules = [
            'id' => 'required|integer',
            'copyId' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取工种参数失败',
            'id.integer' => '工种参数类型错误',
            'copyId.required' => '获取工种参数失败',
            'copyId.integer' => '工种参数类型错误',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['id'] == $param['copyId']){
                throw new ApiException('不能与当前工种相同',240602);
            }
            $assignmentModel = new AssignmentModel();
            $copyId = $param['copyId'];
            unset($param['copyId']);
            $assignment = $assignmentModel->selectLists(['professionId' => $copyId]);
            if(empty($assignment)){
                throw new ApiException('目标工种下没有施工项',240603);
            }
            foreach ($assignment as $a) {
                if ($a->status == 1) {
                    $data = [
                        'professionId' => $param['id'],
                        'name' => $a->name,
                        'unitId' => $a->unitId,
                        'remark' => $a->remark,
                        'status' => 1,
                        'order' => $a->order,
                    ];
                    $assignmentModel->insert($data);
                }
            }
        } else {
            throw new ApiException($validator->errors()->first(),240601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}